package edu.tec.mty.core.modules;

import java.util.Vector;
import java.util.regex.Pattern;

import edu.tec.mty.core.modules.models.Message;

public class Messages {

	private Vector<Message> playersMessages = new Vector<Message>();
	private Vector<Message> refereeMessages = new Vector<Message>();
	private Vector<Message> coachesMessages = new Vector<Message>();
	private Game game;
	//private Player player;
	//private Logger logger = new Logger("messages", "default");
	
	public Messages(Game game, Self player) {
		this.game = game;
		//this.player = player;
	}
	
	public Message getNextPlayerMessage() throws Exception {
		if (this.playersMessages.size() > 0) {
			Message message = this.playersMessages.elementAt(0);
			this.playersMessages.remove(0);
			return message;
		} else {
			throw new Exception("There are no new player messages");
		}
	}
	
	public Message getNextCoachMessage() throws Exception {
		if (this.coachesMessages.size() > 0) {
			Message message = this.coachesMessages.elementAt(0);
			this.coachesMessages.remove(0);
			return message;
		} else {
			throw new Exception("There are no new coach messages");
		}
	}
	
	public void updateMessages(String text) {
		int index = text.indexOf(' ');
		text = text.substring(index + 1);
		index = text.indexOf(' ');
		long time = Long.parseLong(text.substring(0, index));
		text = text.substring(index + 1);
		String sender;
		if (!(text.charAt(0) == '(')) {
			index = text.indexOf(' ');
			sender = text.substring(0, index);
			text = text.substring(index + 1);
		} else {
			index = text.indexOf(')');
			sender = text.substring(1, index);
			text = text.substring(index + 2);
		}
		String message = text.substring(0, text.length() - 1);
		//System.out.println(sender + ": " + message);
		Message msg;
		if (Pattern.matches("\\-?(\\d+|\\d+\\.\\d+)", sender)) { //direction (for players)
			msg = new Message(time, Double.parseDouble(sender), message);
			this.playersMessages.add(msg);
		} else {
			switch (sender.charAt(0)) {
			case 'p': //player (for coach)
				String[] tokens = sender.split(" ");
				msg = new Message(time, tokens[1].substring(1, tokens[1].length() - 1), Integer.parseInt(tokens[2]), message);
				this.playersMessages.add(msg);
				break;
			case 'r': //referee (for coach and players)
				msg = new Message(time, message);
				this.refereeMessages.add(msg);
				this.game.notifyPlayMode(message);
				break;
			case 'o': //online_coach (for players)
				msg = new Message(time, sender, message);
				this.coachesMessages.add(msg);
				//TODO: check if it coach language and notify to player
				break;
			case 'c': //coach (for players)
				msg = new Message(time, sender, message);
				this.coachesMessages.add(msg);
				//TODO: check if it coach language and notify to player
				break;
			case 's': //self
				break;
			default:
				break;
			}
		}
	}
	
	//Test
	
	/*public static void main(String[] args) {
		Messages messages = new Messages("left");
		messages.updateMessages("(hear 10 -34.5 if (directive) then (action))");
	}
	/**/
}
